package qxx.information.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qxx.information.config.enums.DataEnums;
import qxx.information.config.Result;
import qxx.information.entity.CollectInfo;
import qxx.information.pojo.dto.CollectInfoQueryDTO;
import qxx.information.pojo.dto.CollectInfoRecordQueryDTO;
import qxx.information.pojo.vo.CollectInfoRecordVO;
import qxx.information.pojo.vo.CollectInfoVO;
import qxx.information.pojo.vo.CollectStatusInfoVO;
import qxx.information.service.CollectInfoService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 采集信息管理表 前端控制器
 * </p>
 *
 * @author qtx
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/information/collectInfo")
public class CollectInfoController {

    @Autowired
    private CollectInfoService collectInfoService;

    @PostMapping("/insertCollectInfo")
    public Result insertCollectInfo(@RequestBody CollectInfo entity){
        Boolean insert = collectInfoService.insertCollectInfo(entity);
        return insert ? Result.success() : Result.failed(DataEnums.FAILED);
    }

    @PostMapping("/queryCollectInfoRecordList")
    public Result queryCollectInfoRecordList(@RequestBody CollectInfoRecordQueryDTO dto){
        IPage<CollectInfoRecordVO> collectInfoRecordVOS = collectInfoService.queryCollectInfoRecordList(dto);
        return Result.success(collectInfoRecordVOS);
    }

    @GetMapping("/getByIdQueryCollectInfo")
    public Result getByIdQueryCollectInfo(Long id){
        CollectInfoVO byIdQueryCollectInfo = collectInfoService.getByIdQueryCollectInfo(id);
        return Result.success(byIdQueryCollectInfo);
    }


    @PostMapping("/listByCollectInfoPage")
    public Result listByCollectInfoPage(@RequestBody CollectInfoQueryDTO dto, HttpServletRequest request){
        IPage<CollectInfoVO> collectInfoVOIPage = collectInfoService.listByCollectInfoPage(dto,request);
        return Result.success(collectInfoVOIPage);
    }

    @PostMapping("/exportCollectInfo")
    public void exportCollectInfo(HttpServletResponse response,@RequestBody CollectInfoQueryDTO dto) throws ClassNotFoundException {
        collectInfoService.exportCollectInfo(response,dto);
    }

    @PostMapping("/queryCollectStatus")
    public Result<CollectStatusInfoVO> queryCollectStatus(@RequestBody CollectInfoRecordQueryDTO dto){
        return Result.success(collectInfoService.queryCollectStatus(dto));
    }

    @PostMapping("/queryCard")
    public Result<String> getUserId(@RequestBody CollectInfoRecordQueryDTO dto) {
        if (StringUtils.isEmpty(dto.getCollectName())) {
            return Result.failed("Base64字符串不能为空");
        }

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            // 创建一个Post对象
            HttpPost post = new HttpPost("https://api.253.com/open/i/ocr/id-ocr-cl");
                                            //https://api.253.com/open/i/ocr/id-ocr-cl

            // 创建请求参数
            List<NameValuePair> list = new ArrayList<>();
            list.add(new BasicNameValuePair("imageType", "BASE64"));
            list.add(new BasicNameValuePair("appKey", "XjXdUrlp"));
            list.add(new BasicNameValuePair("appId", "qy1Nv5XX"));
            list.add(new BasicNameValuePair("image", dto.getCollectName()));
            list.add(new BasicNameValuePair("ocrType", "0"));

            // 包装成一个entity对象
            StringEntity entity = new UrlEncodedFormEntity(list, "utf-8");
            post.setEntity(entity);

            // 执行请求
            try (CloseableHttpResponse response = client.execute(post)) {
                int code = response.getStatusLine().getStatusCode();
                String content = EntityUtils.toString(response.getEntity());

                if (code == 200) {
                    return Result.success(content);
                } else {
                    return Result.failed("OCR接口调用失败，状态码：" + code + "，响应：" + content);
                }
            }
        } catch (Exception e) {
            return Result.failed("调用OCR接口时发生异常：" + e.getMessage());
        }
    }


}
